<template>
  <div class="line-input">
    <div class="line-input__content">
      <div v-if="label !== ''"
           class="line-input__content__label">
        <span>{{label}}</span>
      </div>
      <div class="line-input__content__input">
        <slot>
          <input @change="$emit('change', currentValue)"
                 ref="input"
                 :placeholder="placeholder"
                 :number="type === 'number'"
                 :type="type"
                 @focus="active = true"
                 :disabled="disabled"
                 :readonly="readonly"
                 :value="currentValue"
                 @input="handleInput">
        </slot>
      </div>
    </div>
    <div class="line-input__line"></div>
  </div>
</template>
<style lang="stylus" scoped>
.line-input {
  position: relative;
  width: 100%;
  background: white;
  height: 80px;

  .line-input__content {
    position: absolute;
    bottom: 1px;
    top: 0;
    left: 30px;
    right: 20px;
    align-items: center;
    display: flex;
    justify-content: flex-start;
    flex-direction: row;

    .line-input__content__label {
      display: flex;

      span {
        font-family: PingFangSC-Regular;
        font-size: 30px;
        color: #333333;
        text-align: center;
      }

      width: 30%;
    }

    .line-input__content__input {
      height: 100%;
      flex: 1;
      display: flex;

      &:nth-child(1) {
        height: 100%;
        width: 100%;
        background: red;
      }

      input {
        font-family: PingFangSC-Regular;
        font-size: 30px;
        color: #333333;
        height: 100%;
        width: 100%;
        border: none;

        &:disabled {
          background: white;
        }
      }
    }
  }

  .line-input__line {
    position: absolute;
    bottom: 0;
    left: 20px;
    right: 20px;
    height: 1px;
    background: RGBA(230, 230, 230, 1);
  }
}
</style>
<script>
export default {
  name: 'line-input',
  data () {
    return { currentValue: this.value }
  },
  props: {
    type: {
      type: String,
      default: 'text'
    },
    rows: String,
    label: String,
    placeholder: String,
    readonly: Boolean,
    disabled: Boolean,
    disableClear: Boolean,
    state: {
      type: String,
      default: 'default'
    },
    value: {},
    attr: Object
  },
  mounted () {},
  watch: {
    value (val) {
      this.currentValue = val
    },

    currentValue (val) {
      this.$emit('input', val)
    },

    attr: {
      immediate: true,
      handler (attrs) {
        this.$nextTick(() => {
          const target = [this.$refs.input, this.$refs.textarea]
          target.forEach(el => {
            if (!el || !attrs) return
            Object.keys(attrs).map(name => el.setAttribute(name, attrs[name]))
          })
        })
      }
    }
  },
  methods: {
    doCloseActive () {
      this.active = false
    },

    handleInput (evt) {
      this.currentValue = evt.target.value
    },

    handleClear () {
      if (this.disabled || this.readonly) return
      this.currentValue = ''
    }
  }
}
</script>
